package com.xca.znb.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.xca.znb.R;
import com.google.android.material.bottomsheet.BottomSheetDialog;
import com.xca.znb.model.impl.ProcurementImpl;
import com.xca.znb.presenter.ProcurementPresenter;

import java.util.ArrayList;
import java.util.List;

public class ProcurementDynamicActivity extends AppCompatActivity {
    private ProcurementPresenter presenter ;
    private BottomSheetDialog bottomSheetDialog;
    private ImageButton OnBackPressed ;
    private TextView showUnit  , showType , showTime ;
    private EditText goodsVariety , goodsDemand , wantLocal , getLocal , goodsDescription ;
    private Button publishBtn ;
    private LinearLayout chanceType , chanceUnit , chanceProcurementTime ;
    @SuppressLint("MissingInflatedId")
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.publish_procurement);
        chanceType = findViewById(R.id.chance_type);
        chanceUnit = findViewById(R.id.chance_unit);
        chanceProcurementTime = findViewById(R.id.chance_procurement_time);
        OnBackPressed = findViewById(R.id.publish_procurement_back_btn) ;
        showUnit = findViewById(R.id.show_unit);
        showType = findViewById(R.id.show_goods_type);
        showTime = findViewById(R.id.show_procurement_time);
        goodsVariety = findViewById(R.id.procurement_goods_variety);
        goodsDemand = findViewById(R.id.procurement_demand) ;
        wantLocal = findViewById(R.id.want_procurement_local) ;
        getLocal = findViewById(R.id.get_goods_local) ;
        goodsDescription = findViewById(R.id.procurement_description);
        publishBtn = findViewById(R.id.publish_procurement_btn) ;


        ProcurementImpl procurement = new ProcurementImpl(this);
        presenter = new ProcurementPresenter(procurement);



        OnBackPressed.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        chanceType.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showBottomSheet1();
            }
        });
        chanceUnit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showBottomSheet2();
            }
        });
        chanceProcurementTime.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showBottomSheet3();
            }
        });

        publishBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String type = showType.getText().toString();
                String variety = goodsVariety.getText().toString();
                String need = goodsDemand.getText().toString();
                String unit = showUnit.getText().toString();
                String want_local = wantLocal.getText().toString();
                String time = showTime.getText().toString();
                String get_local = getLocal.getText().toString();
                String goodsText = goodsDescription.getText().toString();
                SharedPreferences sharedPreferences = getSharedPreferences("loginPhone", Context.MODE_PRIVATE);
                String phone = sharedPreferences.getString("phone" , "");

                if (type.equals("选择货品类型")){
                    Toast.makeText(ProcurementDynamicActivity.this, "请选择货品类型", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (need.isEmpty()){
                    Toast.makeText(ProcurementDynamicActivity.this, "需求量不能为空", Toast.LENGTH_SHORT).show();
                    return;
                }

                if (get_local.isEmpty()){
                    Toast.makeText(ProcurementDynamicActivity.this, "收货地址不能为空", Toast.LENGTH_SHORT).show();
                    return;
                }
                boolean isAdd = presenter.addProcurement(type,variety,need,unit,want_local,time,get_local,goodsText,phone);
                if (isAdd){
                    Toast.makeText(ProcurementDynamicActivity.this, "采购发布成功", Toast.LENGTH_SHORT).show();
                    finish();
                }else {
                    Toast.makeText(ProcurementDynamicActivity.this, "采购发布失败", Toast.LENGTH_SHORT).show();
                }
            }
        });




    }

    private void showBottomSheet1() {
        // 如果BottomSheetDialog还未创建，则创建它
//        if (bottomSheetDialog == null) {
            bottomSheetDialog = new BottomSheetDialog(ProcurementDynamicActivity.this);
            bottomSheetDialog.setContentView(R.layout.procurement_goods_type);

        Button option1 = bottomSheetDialog.findViewById(R.id.goods_option1);
        Button option2 = bottomSheetDialog.findViewById(R.id.goods_option2);
        Button option3 = bottomSheetDialog.findViewById(R.id.goods_option3);
        // 设置按钮点击事件，这里只是示例，实际中可以处理选项的逻辑
        option1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String type = option1.getText().toString();
                showType.setText(type);
                // 处理选项1的点击事件
                bottomSheetDialog.dismiss(); // 关闭BottomSheet
            }
        });
        option2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String type = option2.getText().toString();
                showType.setText(type);
                // 处理选项2的点击事件
                bottomSheetDialog.dismiss(); // 关闭BottomSheet
            }
        });
        option3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String type = option3.getText().toString();
                showType.setText(type);
                // 处理选项2的点击事件
                bottomSheetDialog.dismiss(); // 关闭BottomSheet
            }
        });
//        }

        // 显示BottomSheet
        bottomSheetDialog.show();
    }
    private void showBottomSheet2() {
        // 如果BottomSheetDialog还未创建，则创建它
//        if (bottomSheetDialog == null) {
            bottomSheetDialog = new BottomSheetDialog(ProcurementDynamicActivity.this);
            bottomSheetDialog.setContentView(R.layout.procurement_goods_unit);

        List<Button> buttons = new ArrayList<>();
        buttons.add(bottomSheetDialog.findViewById(R.id.unit1));
        buttons.add(bottomSheetDialog.findViewById(R.id.unit2));
        buttons.add(bottomSheetDialog.findViewById(R.id.unit3));
        buttons.add(bottomSheetDialog.findViewById(R.id.unit4));
        buttons.add(bottomSheetDialog.findViewById(R.id.unit5));
        buttons.add(bottomSheetDialog.findViewById(R.id.unit6));
        buttons.add(bottomSheetDialog.findViewById(R.id.unit7));
        buttons.add(bottomSheetDialog.findViewById(R.id.unit8));
        buttons.add(bottomSheetDialog.findViewById(R.id.unit9));
        buttons.add(bottomSheetDialog.findViewById(R.id.unit10));
        buttons.add(bottomSheetDialog.findViewById(R.id.unit11));
        buttons.add(bottomSheetDialog.findViewById(R.id.unit12));
        buttons.add(bottomSheetDialog.findViewById(R.id.unit13));
        buttons.add(bottomSheetDialog.findViewById(R.id.unit14));
        buttons.add(bottomSheetDialog.findViewById(R.id.unit15));
        buttons.add(bottomSheetDialog.findViewById(R.id.unit16));
        buttons.add(bottomSheetDialog.findViewById(R.id.unit17));
        buttons.add(bottomSheetDialog.findViewById(R.id.unit18));
        buttons.add(bottomSheetDialog.findViewById(R.id.unit19));
        buttons.add(bottomSheetDialog.findViewById(R.id.unit20));
        buttons.add(bottomSheetDialog.findViewById(R.id.unit21));
        buttons.add(bottomSheetDialog.findViewById(R.id.unit22));
        buttons.add(bottomSheetDialog.findViewById(R.id.unit23));
        buttons.add(bottomSheetDialog.findViewById(R.id.unit24));
        buttons.add(bottomSheetDialog.findViewById(R.id.unit25));
        buttons.add(bottomSheetDialog.findViewById(R.id.unit26));
        buttons.add(bottomSheetDialog.findViewById(R.id.unit27));
        buttons.add(bottomSheetDialog.findViewById(R.id.unit28));
        buttons.add(bottomSheetDialog.findViewById(R.id.unit29));
        buttons.add(bottomSheetDialog.findViewById(R.id.unit30));


        // 设置按钮点击事件，这里只是示例，实际中可以处理选项的逻辑
        for (Button button : buttons){
            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String getText = ((Button) v).getText().toString();
                    showUnit.setText(getText);
                    bottomSheetDialog.dismiss();
                }
            });
        }
//        }

        // 显示BottomSheet
        bottomSheetDialog.show();
    }
    private void showBottomSheet3() {
        // 如果BottomSheetDialog还未创建，则创建它
//        if (bottomSheetDialog == null) {
            bottomSheetDialog = new BottomSheetDialog(ProcurementDynamicActivity.this);
            bottomSheetDialog.setContentView(R.layout.procurement_goods_time);

            // 找到BottomSheet中的按钮，并设置点击事件
            Button option1 = bottomSheetDialog.findViewById(R.id.procurement_time1);
            Button option2 = bottomSheetDialog.findViewById(R.id.procurement_time2);
            Button option3 = bottomSheetDialog.findViewById(R.id.procurement_time3);
            Button option4 = bottomSheetDialog.findViewById(R.id.procurement_time4);
            // 设置按钮点击事件，这里只是示例，实际中可以处理选项的逻辑
            option1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    // 处理选项1的点击事件\
                    String time = option1.getText().toString();
                    showTime.setText(time);
                    bottomSheetDialog.dismiss(); // 关闭BottomSheet
                }
            });
            option2.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    // 处理选项2的点击事件
                    String time = option2.getText().toString();
                    showTime.setText(time);
                    bottomSheetDialog.dismiss(); // 关闭BottomSheet
                }
            });
        option3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 处理选项2的点击事件
                String time = option3.getText().toString();
                showTime.setText(time);
                bottomSheetDialog.dismiss(); // 关闭BottomSheet
            }
        });
        option4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 处理选项2的点击事件
                String time = option4.getText().toString();
                showTime.setText(time);
                bottomSheetDialog.dismiss(); // 关闭BottomSheet
            }
        });
//        }

        // 显示BottomSheet
        bottomSheetDialog.show();
    }
}
